<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<!--{include file='comm/header.tpl'}-->
<!--{if $area_array}-->
    <title>配送区域修改</title>
<!--{else}-->
    <title>配送区域新增</title>
<!--{/if}-->

<style type="text/css">
.area_box {
    display: inline-block;
    height: 16px;
    overflow: hidden;
    width: 200px;
}
</style>

</head>

<body>
	<div style="width:100%">

		<div class="items clearfix" style="padding-bottom: 20px;">

			<h2 style="" class="tal">
				<!--{if $area_array}-->
                    配送区域修改
			    <!--{else}-->
                    配送区域新增
			    <!--{/if}-->
			</h2>
			
			<div class="items2">

				<form id="addForm" name="addForm" onsubmit="return false;">

                    <div class="select-box">
                        <div>
                            配送区域基本信息
                        </div>
                    </div>

					<table>
						<tr>
							<td width="150" class="tar">配送区域名称：</td>
							<td width="*">
								<input type="text" id="send_region_name" name="send_region_name" value="<!--{$send_area_array.send_region_name}-->" style="width: 300px;">
								<input type="hidden" id="send_region_id" name="send_region_id" value="<!--{$send_area_array.send_region_id|default:0}-->">
								<input type="hidden" id="sendTypeID" name="sendTypeID" value="<!--{$send_type_array.sendTypeID}-->">
							</td>
						</tr>
                        <tr>
                            <td class="tar">首重：</td>
                            <td>
                                <input type="text" style="width:70px" id="ykg" name="ykg" value="<!--{$send_area_array.ykg}-->">&nbsp;<span class="red">KG</span>
                            </td>
                        </tr>
						<tr>
							<td class="tar">首重运费：</td>
							<td>
								<input type="text" style="width:200px" id="freight" name="freight" value="<!--{$send_area_array.freight}-->">&nbsp;<span class="red">元</span>
							</td>
						</tr>

						<tr>
							<td class="tar">超重运费：</td>
							<td>
								<input type="text" style="width:200px" id="overweight_freight" name="overweight_freight" value="<!--{$send_area_array.overweight_freight}-->">&nbsp;<span class="red">元/KG</span>
							</td>
						</tr>

					</table>

                    <div class="select-box" style="margin-top: 10px;">
                        <div>
                            区域列表
                        </div>
                    </div>

                    <table>
                        <tbody>
                        <tr>
                            <td>
                                <div style="height:230px;float:left;">省：</div>
                                <div style="height:230px;float:left;margin-right:20px;">
                                    <select id="province_list" style="width:200px;height:150px;" size="20"></select>
                                    <div style="margin: 10px 0;">
                                        <input type="button" class="btn" value="添　　加" onclick="area.select.single(1);">
                                    </div>
                                    <div>
                                        <input type="button" class="btn" value="添加全部" onclick="area.select.all(1);">
                                    </div>
                                </div>
                                <div style="height:230px;float:left;">市：</div>
                                <div style="height:230px;float:left;margin-right:20px;">
                                    <select id="city_list" style="width:200px;height:150px;" size="20"></select>
                                    <div style="margin: 10px 0;">
                                        <input type="button" class="btn" value="添　　加" onclick="area.select.single(2);">
                                    </div>
                                    <div>
                                        <input type="button" class="btn" value="添加全部" onclick="area.select.all(2);">
                                    </div>
                                </div>
                                <div style="height:230px;float:left;">区：</div>
                                <div style="height:230px;float:left;margin-right:20px;">
                                    <select id="county_list" style="width:200px;height:150px;" size="20"></select>
                                    <div style="margin: 10px 0;">
                                        <input type="button" class="btn" value="添　　加" onclick="area.select.single(3);">
                                    </div>
                                    <div>
                                        <input type="button" class="btn" value="添加全部" onclick="area.select.all(3);">
                                    </div>
                                </div>
                                <div style="clear:both;"></div>
                            </td>
                        </tr>
                        </tbody>
                    </table>

                    <table>
                        <tbody>
                        <tr>
                            <td width="110">所选区域（省）：</td>
                            <td width="*" id="province"></td>
                        </tr>
                        <tr>
                            <td>所选区域（市）：</td>
                            <td id="city"></td>
                        </tr>
                        <tr>
                            <td>所选区域（区）：</td>
                            <td id="country"></td>
                        </tr>
                        </tbody>
                    </table>

				</form>
				
			</div>
		</div>
		
		<div class="bottom_fixbox">
			<div class="rectbox tac">
                <!--{if $area_array}-->
	        		<input type="button" class="btn" onclick="area.edit.check();" value="修改配送区域">
				<!--{else}-->
					<input type="button" class="btn" onclick="area.edit.check();" value="添加配送区域">
				<!--{/if}-->
				<input type="button" class="btn" onclick="E.closeWindows();" value="关闭">
	        </div>
		</div>
	</div>


<script type="text/javascript" src="/framework/ebsig/js/region.min.js"></script>
<script type="text/javascript" src="/framework/ebsig/js/region.change.min.js"></script>

<script type="text/javascript">

    E.region.init();

    var area = {

        county_obj: {},
        city_obj: {},
        province_obj: {},


        create: {

            province: function( args ) {
                var html = "<span class=\"area_box\"><input type=\"checkbox\" name=\"province_box\" value=\"" + args.province_id + "\" checked=\"checked\" />&nbsp;"
                           + args.province_name + "</span>";
                $("#province").append(html);
                area.province_obj[args.province_id] = args.province_name;
            },

            city: function( args ) {
                if (!args.province_name)
                    args.province_name = $("#province_list").find("option:selected").text();
                var html = "<span class=\"area_box\"><input type=\"checkbox\" name=\"city_box\" value=\"" + args.city_id + "\" checked=\"checked\" />&nbsp;"
                           + args.province_name + "-" + args.city_name + "</span>";
                $("#city").append(html);
                area.city_obj[args.city_id] = args.city_name;
            },

            county: function( args ) {
                if (!args.province_name)
                    args.province_name = $("#province_list").find("option:selected").text();
                if (!args.city_name)
                    args.city_name = $("#city_list").find("option:selected").text();
                var html = "<span class=\"area_box\"><input type=\"checkbox\" name=\"county_box\" value=\"" + args.county_id + "\" checked=\"checked\" />&nbsp;"
                           + args.province_name + "-" + args.city_name + "-" + args.county_name + "</span>";
                $("#country").append(html);
                area.county_obj[args.county_id] = args.county_name;
            }

        },

        select: {

            single: function( id ) {

                //查询省份
                if (id == 1) {

                    var province_id = $("#province_list").val();
                    if (!province_id) {
                        E.alert("请选择省份");
                        return false;
                    } else {
                        if (area.province_obj[province_id]) {
                            E.alert("省份已选择");
                            return false;
                        }
                        var province_name = $("#province_list").find("option:selected").text();
                        area.create.province({
                            province_id: province_id,
                            province_name: province_name
                        });
                    }

                }

                //查询市区
                if (id == 2) {

                    var city_id = $("#city_list").val();
                    if (!city_id) {
                        E.alert("请选择市区");
                        return false;
                    } else {
                        var province_id = $("#province_list").val();
                        if (area.province_obj[province_id]) {
                            E.alert("已选择该市区的上级区域");
                            return false;
                        }
                        if (area.city_obj[city_id]) {
                            E.alert("市区已选择");
                            return false;
                        }
                        var city_name = $("#city_list").find("option:selected").text();
                        area.create.city({
                            city_name: city_name,
                            city_id: city_id
                        });
                    }

                }

                //查询地区
                if (id == 3) {

                    var county_id = $("#county_list").val();
                    if (!county_id) {
                        E.alert("请选择地区");
                        return false;
                    } else {
                        var province_id = $("#province_list").val();
                        if (area.province_obj[province_id]) {
                            E.alert("已选择该地区的上级区域");
                            return false;
                        }
                        var city_id = $("#city_list").val();
                        if (area.city_obj[city_id]) {
                            E.alert("已选择该地区的上级区域");
                            return false;
                        }
                        if (area.county_obj[county_id]) {
                            E.alert("地区已选择");
                            return false;
                        }
                        var county_name = $("#county_list").find("option:selected").text();
                        area.create.county({
                            county_id: county_id,
                            county_name: county_name
                        });
                    }

                }

                return true;

            },

            all: function( id ) {

                if (id == 1) {

                    $.each($("#province_list").find("option"), function(k, v) {

                        var province_id = $(v).val();
                        if (area.province_obj[province_id])
                            return true;
                        area.create.province({
                            province_id: province_id,
                            province_name: $(v).text()
                        })

                    });

                }

                if (id == 2) {

                    $.each($("#city_list").find("option"), function(k, v) {

                        var province_id = $("#province_list").val();
                        if (area.province_obj[province_id]) {
                            E.alert("已选择该地区的上级区域");
                            return false;
                        }

                        var city_id = $(v).val();
                        if (area.city_obj[city_id])
                            return true;

                        area.create.city({
                            city_id: city_id,
                            city_name: $(v).text()
                        })
                    });

                }


                if (id == 3) {

                    $.each($("#county_list").find("option"), function(k, v) {

                        var province_id = $("#province_list").val();
                        if (area.province_obj[province_id]) {
                            E.alert("已选择该地区的上级区域");
                            return false;
                        }
                        var city_id = $("#city_list").val();
                        if (area.city_obj[city_id]) {
                            E.alert("已选择该地区的上级区域");
                            return false;
                        }

                        var county_id = $(v).val();
                        if (area.county_obj[county_id])
                            return true;

                        area.create.county({
                            county_id: county_id,
                            county_name: $(v).text()
                        })

                    });

                }


            }

        },

        edit: {

            check: function() {

                this.dt = E.getFormValues("addForm");
                var error_msg = "";

                //配送区域名称
                if (E.isEmpty(this.dt.send_region_name))
                    error_msg += "配送区域名称不能为空<br>";

                //首重
                if (E.isEmpty(this.dt.ykg))
                    error_msg += "首重不能为空<br>";
                else if (!E.isDigital(this.dt.ykg) || this.dt.ykg <= 0)
                    error_msg += "首重必须是大于0的整数<br>";

                //首重运费
                if (E.isEmpty(this.dt.freight))
                    error_msg += "首重运费不能为空<br>";
                else if (!E.isMoney(this.dt.freight) || this.dt.freight < 0)
                    error_msg += "首重运费必须是大于等于0的数字<br>";

                //超重运费
                if (E.isEmpty(this.dt.overweight_freight))
                    error_msg += "超重运费不能为空<br>";
                else if (!E.isMoney(this.dt.overweight_freight) || this.dt.overweight_freight < 0)
                    error_msg += "超重运费必须是大于等于0的数字<br>";

                //区域
                if (!this.dt.province_box)
                    this.dt.province_box = new Array();
                if (!this.dt.city_box)
                    this.dt.city_box = new Array();
                if (!this.dt.county_box)
                    this.dt.county_box = new Array();
                if (this.dt.province_box.length == 0 && this.dt.city_box.length == 0 && this.dt.county_box.length == 0)
                    error_msg += "请选择配送区域";

                if (error_msg != "") {
                    E.alert(error_msg);
                } else {

                    if (this.dt.send_region_id == 0) {
                        E.loadding.open("正在新增配送区域，请稍候...");
                    } else {
                        E.loadding.open("正在新增配送区域，请稍候...");
                    }

                    E.ajax_post({
                        url: "/invokingAjax-backend/system/sendType.ajax?operFlg=6",
                        data: this.dt,
                        call: "area.edit.result"
                    });

                }

            },

            result: function( o ) {

                E.loadding.close();

                if (o.code == 200) {
                    E.opener("area.load");
                    if (this.dt.send_region_id == 0)
                        E.alert(o.message, 2, "E.refresh");
                    else
                        E.alert(o.message, 2, "E.closeWindows");
                } else {
                    E.alert(o.message);
                }

            }

        }

    };

    $(document).ready(function() {

        $(document).on("click", "input[name='province_box']", function() {

            delete area.province_obj[$(this).val()];
            $(this).parent().remove();

        }).on("click", "input[name='city_box']", function() {

            delete area.city_obj[$(this).val()];
            $(this).parent().remove();

        }).on("click", "input[name='county_box']", function() {

            delete area.county_obj[$(this).val()];
            $(this).parent().remove();

        })

    });


    <!--{if $send_area_list_array}-->

    <!--{foreach from=$send_area_list_array item=list}-->
        <!--{if $list.levelFlg == 1}-->
        area.create.province({
            province_id: <!--{$list.regionID}-->,
            province_name: "<!--{$list.province_name}-->"
        });
        <!--{elseif $list.levelFlg == 2}-->
        area.create.city({
            city_id: <!--{$list.regionID}-->,
            province_name: "<!--{$list.province_name}-->",
            city_name: "<!--{$list.city_name}-->"
        });
        <!--{elseif $list.levelFlg == 3}-->
        area.create.county({
            county_id: <!--{$list.regionID}-->,
            province_name: "<!--{$list.province_name}-->",
            city_name: "<!--{$list.city_name}-->",
            county_name: "<!--{$list.county_name}-->"
        });
        <!--{/if}-->
    <!--{/foreach}-->

    <!--{/if}-->


</script>

</body>
</html>